/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WebServiceLoginClient;

import WebServiceModel.GamingStateModel;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;

import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.SOAPException;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;

/**
 *
 * @author Peach
 */
public class LoginClient
{

    public LoginClient()
    {
        try
        {
            url = new URL("http://59.78.37.65:8080/soap/servlet/rpcrouter");
        } catch (MalformedURLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        uri = "loginServer";
        methodLogin = "login";
        methodLogout = "logout";
    }

    public LoginClient(String URI, String methodLoginString,
            String methodLogoutString)
    {
        try
        {
            url = new URL("http://59.78.37.65:8080/soap/servlet/rpcrouter");
        } catch (MalformedURLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        uri = URI;
        methodLogin = methodLoginString;
        methodLogout = methodLogoutString;
    }

    public boolean TryConnect()
    {

        try
        {
            Call call = new Call();
            call.setTargetObjectURI(uri);
            call.setMethodName("TestLink");
            call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
            Vector params = new Vector();

            call.setParams(params);
            try
            {
                Response resp = call.invoke(url, "");
                if (resp.generatedFault())
                {
                    Fault fault = resp.getFault();
                    System.out.println("The call failed:");
                    System.out.println("Fault code =" + fault.getFaultCode());
                    System.out.println("Fault String =" + fault.getFaultString());
                } else
                {
                    Parameter result = resp.getReturnValue();
                    //System.out.println(result.getValue());
                    Integer connect = (Integer) result.getValue();
                    if (connect == 1)
                    {
                        return true;
                    }
                }
            } catch (SOAPException e)
            {
                // TODO Auto-generated catch block            
                e.printStackTrace();

                return false;
            }
        } catch (Error e)
        {
            e.printStackTrace();
            return false;
        }
        return false;

    }

    public int login(String detail, String userId, String password)
    {
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodLogin);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("detail", String.class, detail, null));
        params.addElement(new Parameter("userId", String.class, userId, null));
        params.addElement(new Parameter("password",String.class,password,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
                //System.out.println(result.getValue());
                return (Integer)result.getValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
        return 0;
    }

    public int logout(String userId)
    {
        Call call = new Call();
        call.setTargetObjectURI(uri);
        call.setMethodName(methodLogout);
        call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
        Vector params = new Vector();
        params.addElement(new Parameter("userId", String.class, userId, null));
        call.setParams(params);
        try
        {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault())
            {
                Fault fault = resp.getFault();
                System.out.println("The call failed:");
                System.out.println("Fault code =" + fault.getFaultCode());
                System.out.println("Fault String =" + fault.getFaultString());
            } else
            {
                Parameter result = resp.getReturnValue();
                //System.out.println(result.getValue());
                return (Integer) result.getValue();
            }
        } catch (SOAPException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }

    
    public static void main(String args[]) throws Exception {
        LoginClient test=new LoginClient();
        test.logout("1");
    }
    
    private URL url;
    private String uri;
    private String methodLogin;
    private String methodLogout;
}
